package com.jdbc.mysql.demo;

import com.jdbc.mysql.util.DBHelper;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 微信公众号：翌孜独秀
 * 微信号：mannertwo
 *
 * @author 小匠
 * 如有问题加我微信号：mannerone
 */
public class DBHelperTest {

    //查询
    public static void select(){
        DBHelper db = new DBHelper();
        //问号“？” 表示你要传入的产数 表示id=1 的数据
        ResultSet rs=db.executeQuery("select * from tb_hero where id= ?",1);
        try {
            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                String address = rs.getString("address");
                String remark = rs.getString("remark");

                // 输出数据
                System.out.print("ID: " + id+"，");
                System.out.print("姓名: " + name+"，");
                System.out.print("性别: " + sex+"，");
                System.out.print("年龄: " + age+"，");
                System.out.print("生日: " + birthday+"，");
                System.out.print("地址: " + address+"，");
                System.out.print("备注: " + remark);
                System.out.print("\n");
            }
        }catch (SQLException err) {
            err.printStackTrace();
        }
    }

    //添加数据 修改删除 与添加一样
    public static void add(){
        DBHelper db = new DBHelper();
        //问号“？” 表示你要传入的产数 表示id=1 的数据
        int temp=db.executeNonQuery("insert into tb_hero(id, name, sex, age,birthday,address,remark) values(5,'亚索','男','12','2016-05-02','a','b')");
        if(temp<=0){
            System.out.println("添加失败");
        }else{
            System.out.println("添加成功");
        }
    }

    //main方法 其他方法可以都去尝试
    public static void main(String[] args) {
        add();
    }
}
